<?php
namespace adminmatrix\plugin\command\migrate;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument as InputArgument;
use think\console\input\Option as InputOption;
use think\console\Output;
use LogicException;
class Create extends  Command
{
    protected function configure()
    {
        $this->setName("plugin:migrate_create")
            ->addOption("plugin",'-p' , InputOption::VALUE_REQUIRED , "插件名称")
            ->setDescription("创建插件数据库迁移")
            ->addArgument('name', InputArgument::REQUIRED, '迁移名称')
        ;
//        parent::configure(); // TODO: Change the autogenerated stub
    }

    protected function execute(Input $input, Output $output)
    {
        /**
         * @var Creator $creator
         */
        $creator = $this->app->get("plugin.migrate.creator");
        $className = $input->getArgument('name');
        $plugin_name = $input->getOption('plugin');
        if (is_null($plugin_name))
            throw new LogicException("插件名称不能为空 --plugin=插件名 或 -p 插件名");
        # 判断插件是否存在
        if (!file_exists($this->app->getRootPath() . "plugins/$plugin_name"))
            throw new LogicException(sprintf("插件 %s 不存在" , $plugin_name) );

        $path = $creator->create($className , $plugin_name);
        $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
    }
}